<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>家长管理页面 - 儿童激励打卡系统</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/Chart.js/3.9.1/chart.min.js"></script>
    <style>
        .gradient-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-shadow {
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- 导航栏 -->
    <nav class="gradient-bg text-white shadow-lg">
        <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex justify-between h-16">
                <div class="flex items-center">
                    <i class="fas fa-star text-2xl mr-3"></i>
                    <h1 class="text-xl font-bold">家长管理面板</h1>
                </div>
                <div class="flex items-center space-x-4">
                    <span class="text-sm">欢迎回来！</span>
                    <a href="/logout" class="bg-white bg-opacity-20 px-4 py-2 rounded-lg hover:bg-opacity-30 transition">
                        <i class="fas fa-sign-out-alt mr-1"></i>退出
                    </a>
                </div>
            </div>
        </div>
    </nav>

    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
        <!-- 统计卡片 -->
        <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
            <div class="bg-white rounded-lg p-6 card-shadow">
                <div class="flex items-center">
                    <div class="p-3 rounded-full bg-blue-100 text-blue-600">
                        <i class="fas fa-trophy text-xl"></i>
                    </div>
                    <div class="ml-4">
                        <p class="text-sm font-medium text-gray-600">总积分</p>
                        <p class="text-2xl font-bold text-gray-900" id="totalPoints">-</p>
                    </div>
                </div>
            </div>
            
            <div class="bg-white rounded-lg p-6 card-shadow">
                <div class="flex items-center">
                    <div class="p-3 rounded-full bg-green-100 text-green-600">
                        <i class="fas fa-chart-line text-xl"></i>
                    </div>
                    <div class="ml-4">
                        <p class="text-sm font-medium text-gray-600">月度完成率</p>
                        <p class="text-2xl font-bold text-gray-900" id="monthlyRate">-</p>
                    </div>
                </div>
            </div>
            
            <div class="bg-white rounded-lg p-6 card-shadow">
                <div class="flex items-center">
                    <div class="p-3 rounded-full bg-yellow-100 text-yellow-600">
                        <i class="fas fa-medal text-xl"></i>
                    </div>
                    <div class="ml-4">
                        <p class="text-sm font-medium text-gray-600">获得徽章</p>
                        <p class="text-2xl font-bold text-gray-900" id="totalBadges">-</p>
                    </div>
                </div>
            </div>
            
            <div class="bg-white rounded-lg p-6 card-shadow">
                <div class="flex items-center">
                    <div class="p-3 rounded-full bg-purple-100 text-purple-600">
                        <i class="fas fa-calendar-check text-xl"></i>
                    </div>
                    <div class="ml-4">
                        <p class="text-sm font-medium text-gray-600">连续打卡</p>
                        <p class="text-2xl font-bold text-gray-900" id="streakDays">7天</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- 图表和建议 -->
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
            <!-- 趋势图表 -->
            <div class="bg-white rounded-lg p-6 card-shadow">
                <h3 class="text-lg font-semibold text-gray-900 mb-4">
                    <i class="fas fa-chart-area mr-2 text-blue-600"></i>完成率趋势
                </h3>
                <canvas id="trendChart" width="400" height="200"></canvas>
            </div>
            
            <!-- 管理建议 -->
            <div class="bg-white rounded-lg p-6 card-shadow">
                <h3 class="text-lg font-semibold text-gray-900 mb-4">
                    <i class="fas fa-lightbulb mr-2 text-yellow-600"></i>管理建议
                </h3>
                <div id="suggestions" class="space-y-4">
                    <!-- 动态加载建议 -->
                </div>
            </div>
        </div>

        <!-- 任务详情 -->
        <div class="mt-8 bg-white rounded-lg p-6 card-shadow">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">
                <i class="fas fa-tasks mr-2 text-green-600"></i>近期任务完成情况
            </h3>
            <div class="overflow-x-auto">
                <table class="min-w-full divide-y divide-gray-200">
                    <thead>
                        <tr>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">日期</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">任务</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">状态</th>
                            <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">积分</th>
                        </tr>
                    </thead>
                    <tbody id="recentTasks" class="bg-white divide-y divide-gray-200">
                        <!-- 动态加载任务 -->
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <script>
        let chart;

        async function loadParentData() {
            try {
                const response = await fetch('/api/parent/data');
                const data = await response.json();
                
                // 更新统计卡片
                document.getElementById('totalPoints').textContent = data.total_points;
                document.getElementById('monthlyRate').textContent = data.monthly_rate + '%';
                document.getElementById('totalBadges').textContent = data.badges.length;
                
                // 更新图表
                updateChart(data.completion_rates);
                
                // 更新建议
                updateSuggestions(data.suggestions);
                
                // 更新任务列表
                updateTasksList(data.completion_rates);
                
            } catch (error) {
                console.error('加载数据失败:', error);
            }
        }

        function updateChart(rates) {
            const ctx = document.getElementById('trendChart').getContext('2d');
            
            if (chart) {
                chart.destroy();
            }
            
            chart = new Chart(ctx, {
                type: 'line',
                data: {
                    labels: rates.map(r => r.date.substring(5)),
                    datasets: [{
                        label: '完成率 (%)',
                        data: rates.map(r => r.rate),
                        borderColor: 'rgb(99, 102, 241)',
                        backgroundColor: 'rgba(99, 102, 241, 0.1)',
                        tension: 0.4,
                        fill: true
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    plugins: {
                        legend: {
                            display: false
                        }
                    },
                    scales: {
                        y: {
                            beginAtZero: true,
                            max: 100
                        }
                    }
                }
            });
        }

        function updateSuggestions(suggestions) {
            const container = document.getElementById('suggestions');
            container.innerHTML = '';
            
            suggestions.forEach(suggestion => {
                const div = document.createElement('div');
                div.className = `p-4 rounded-lg border-l-4 ${
                    suggestion.type === 'success' ? 'bg-green-50 border-green-400' :
                    suggestion.type === 'warning' ? 'bg-yellow-50 border-yellow-400' :
                    'bg-blue-50 border-blue-400'
                }`;
                
                div.innerHTML = `
                    <div class="flex">
                        <div class="flex-shrink-0">
                            <i class="fas fa-${suggestion.type === 'success' ? 'check-circle' : 
                                           suggestion.type === 'warning' ? 'exclamation-triangle' : 
                                           'info-circle'} text-${suggestion.type === 'success' ? 'green' : 
                                                                suggestion.type === 'warning' ? 'yellow' : 'blue'}-400"></i>
                        </div>
                        <div class="ml-3">
                            <h4 class="text-sm font-medium text-gray-900">${suggestion.title}</h4>
                            <p class="text-sm text-gray-600 mt-1">${suggestion.content}</p>
                        </div>
                    </div>
                `;
                
                container.appendChild(div);
            });
        }

        function updateTasksList(rates) {
            // 这里简化处理，实际应该从API获取详细任务数据
            const container = document.getElementById('recentTasks');
            container.innerHTML = '';
            
            rates.slice(-5).reverse().forEach(day => {
                const tasks = ['完成作业', '整理房间', '阅读30分钟'];
                tasks.forEach(taskName => {
                    const tr = document.createElement('tr');
                    tr.innerHTML = `
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">${day.date}</td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">${taskName}</td>
                        <td class="px-6 py-4 whitespace-nowrap">
                            <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full ${
                                Math.random() > 0.5 ? 'bg-green-100 text-green-800' : 'bg-red-100 text-red-800'
                            }">
                                ${Math.random() > 0.5 ? '已完成' : '未完成'}
                            </span>
                        </td>
                        <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">${Math.floor(Math.random() * 10) + 5}</td>
                    `;
                    container.appendChild(tr);
                });
            });
        }

        // 页面加载时获取数据
        loadParentData();
        
        // 每30秒刷新一次数据
        setInterval(loadParentData, 30000);
    </script>
</body>
</html>
